<?php

class Configurator{
	private $host;
	private $databasename;
	private $user;
	private $pass;
	private $path=".";
	public function setDataBaseInfo($host,$databasename){
		$this->host=$host;
		$this->databasename=$databasename;
		$buffer = fopen($this->path."/config_i.php", "w");
		$linea="<?php\n";
		fwrite($buffer, $linea);
		$linea="\$HOST = '$this->host';\n";
		fwrite($buffer,$linea);
		$linea="\$BASEDEDATOS = '$this->databasename';\n";
		fwrite($buffer,$linea);
		/*$linea="?>";
		fwrite($buffer,$linea);*/
		fclose($buffer) ;
	}
	public function setDataBaseUser($user,$pass){
		$this->user=$user;
		$this->pass=$pass;
		$buffer = fopen($this->path."/config_i.php", "a");
		/*$linea="<?php\n";
		fwrite($buffer, $linea);*/
		$linea="\$USUARIO = '$this->user';\n";
		fwrite($buffer, $linea);
		$linea="\$PASS = '$this->pass';\n";
		fwrite($buffer, $linea);
		$linea="?>";
		fwrite($buffer,$linea);
		fclose($buffer) ;
	}
	public function loadSQL($path){
		$sqlquery;
		if (file_exists($path))
			$SQLFile = file($path);

		foreach ($SQLFile as $linea){
			$tmpquery.=$linea."\n";
		}
		$this->myconn = mysql_connect($this->host,$this->user,$this->pass);
		if (! $this->myconn){
			echo "Error al intentar conectarse con el servidor MySQL";
		exit(); 
		}

		if (! @mysql_select_db($this->databasename,$this->myconn)){
			echo "No se pudo conectar correctamente con la Base de datos";
			exit();
		}
		$query=split(";",$tmpquery);
		foreach ($query as $sqlquery){
			mysql_query($sqlquery.";") or die("Error al ejecutar la sentencia ".$sqlquery);
		}		
	}
	public function setSysUser($user,$pass){
		$this->myconn = mysql_connect($this->host,$this->user,$this->pass);
		if (! $this->myconn){
			echo "Error al intentar conectarse con el servidor MySQL";
		exit(); 
		}

		if (! @mysql_select_db("system",$this->myconn)){
			echo "No se pudo conectar correctamente con la Base de datos";
			exit();
		}
		$sqlquery="INSERT INTO `sys_users` ( `USER_ID` , `ROL_ID` , `FIRST_NAMES` , `LAST_NAMES` , `PASSWORD` ) VALUES ('1', '1', '$user', NULL , '$pass');";
		mysql_query($sqlquery) or die ("Error al agregar usuario");
	}
	public function setAppPath($path){
		$buffer = fopen($this->path."/admin/config/general_inc2.php", "w");
		$linea="<?php\n";
		fwrite($buffer, $linea);
		$pathc=str_replace("/","\\\\",$path);
		echo $pathc;
		$linea="\$strProgramPath='$pathc';\n";
		fwrite($buffer, $linea);
		$linea="?>";
		fwrite($buffer,$linea);
		fclose($buffer) ;		
	}
}
?>